<!--  -->
<template>
  <div class="login">
    <div class="login-box">
      <div class="icon">
        <img src="../assets/logo.png" alt="" />
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginFormRules">
        <el-form-item id="user" prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item id="password" prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item id="btn">
          <el-button @click="login">登录</el-button>
          <el-button @click="reset">重置</el-button>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录账号', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入登录密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ]
      }
    }
  },

  components: {},

  methods: {
    reset () {
      this.$refs.loginForm.resetFields()
    },
    login () {
      this.$refs.loginForm.validate(async valide => {
        if (!valide) return
        const { data } = await this.$http.post('login', this.loginForm)
        console.log(data)
        if (data.meta.status !== 200) return this.$message.error('登录失败！')
        this.$message.success('登录成功!!')
        window.sessionStorage.setItem('token', data.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style scoped>
.login{
  background-color: #2b4b6b;
  height: 100%;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: #fff;
  position: absolute;
  left: 50%;
  right: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.icon {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  /* left: 50%; */
  transform: translate(50%,-50%);
  margin-left: 85px;
}
.icon img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}
#user,#password,#btn{
  position: relative;
  top: 100px;
  padding: 0 10px;
}
#btn{
  display: flex;
  justify-content: flex-end;
}
</style>
